# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: db\data\system_config.py
import logging
from sqlalchemy import select as sql_select
from db import Connection
from db.tables.system_config import SystemConfigRow

logger = logging.getLogger("db.data.system_config")


def get_system_config(shard, name, default=None):
    with Connection(shard) as (db):
        q = sql_select((SystemConfigRow.value,)).where(SystemConfigRow.name == name)
        result = db.execute(q).fetchone()
        if result:
            return result.value
        return default


def get_proxy(shard):
    v = get_system_config(shard, "proxy")
    if isinstance(v, dict):
        if v.get("enabled") is True:
            proxy = dict(
                port=v.get("port", 8080),
                address=v.get("address", "localhost"),
                username=v.get("username"),
                password=v.get("password"),
                protocol=v.get("protocol", "http"),
            )
            auth = ""
            if proxy["username"]:
                auth = proxy["username"]
                if proxy["password"]:
                    auth = auth + ":" + proxy["password"]
                auth += "@"
            requests_proxy = None
            if proxy["protocol"] == "http":
                requests_proxy = {
                    "http": "http://%s%s:%s" % (auth, proxy["address"], proxy["port"]),
                    "https": "http://%s%s:%s" % (auth, proxy["address"], proxy["port"]),
                }
            proxy["requests_proxy"] = requests_proxy
            return proxy


def get_requests_proxy(shard):
    proxy = get_proxy(shard=shard)
    if isinstance(proxy, dict):
        return proxy.get("requests_proxy")
    else:
        return proxy
